/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2018 University of Padova
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Authors: Davide Magrin <magrinda@dei.unipd.it>
 *          Martina Capuzzo <capuzzom@dei.unipd.it>
 *
 * Modified by: Bastien Tauran <bastien.tauran@viveris.fr>
 */

#include "lora-network-server.h"

#include "lora-device-address.h"
#include "lora-frame-header.h"
#include "lora-network-status.h"
#include "lorawan-mac-command.h"
#include "lorawan-mac-end-device-class-a.h"
#include "lorawan-mac-header.h"
#include "satellite-lorawan-net-device.h"
#include "satellite-topology.h"

#include "ns3/singleton.h"
#include <ns3/log.h>

namespace ns3
{

NS_LOG_COMPONENT_DEFINE("LoraNetworkServer");

NS_OBJECT_ENSURE_REGISTERED(LoraNetworkServer);

TypeId
LoraNetworkServer::GetTypeId(void)
{
    static TypeId tid =
        TypeId("ns3::LoraNetworkServer")
            .SetParent<Application>()
            .AddConstructor<LoraNetworkServer>()
            .AddTraceSource(
                "ReceivedPacket",
                "Trace source that is fired when a packet arrives at the Network Server",
                MakeTraceSourceAccessor(&LoraNetworkServer::m_receivedPacket),
                "ns3::Packet::TracedCallback");
    return tid;
}

LoraNetworkServer::LoraNetworkServer()
    : m_status(Create<LoraNetworkStatus>()),
      m_controller(Create<LoraNetworkController>(m_status)),
      m_scheduler(Create<LoraNetworkScheduler>(m_status, m_controller))
{
    NS_LOG_FUNCTION_NOARGS();
}

LoraNetworkServer::~LoraNetworkServer()
{
    NS_LOG_FUNCTION_NOARGS();
}

void
LoraNetworkServer::StartApplication(void)
{
    NS_LOG_FUNCTION_NOARGS();
}

void
LoraNetworkServer::StopApplication(void)
{
    NS_LOG_FUNCTION_NOARGS();
}

void
LoraNetworkServer::AddGateway(Ptr<Node> gateway, Ptr<NetDevice> netDevice)
{
    NS_LOG_FUNCTION(this << gateway);

    // Get the PointToPointNetDevice
    Ptr<PointToPointNetDevice> p2pNetDevice;
    for (uint32_t i = 0; i < gateway->GetNDevices(); i++)
    {
        p2pNetDevice = gateway->GetDevice(i)->GetObject<PointToPointNetDevice>();
        if (p2pNetDevice != nullptr)
        {
            // We found a p2pNetDevice on the gateway
            break;
        }
    }

    // Get the Address
    Address gatewayAddress = p2pNetDevice->GetAddress();

    // Get the gateway's LoRa MAC layer (assumes gateway's MAC is configured as first device)
    switch (Singleton<SatTopology>::Get()->GetForwardLinkRegenerationMode())
    {
    case SatEnums::TRANSPARENT: {
        Ptr<SatLorawanNetDevice> satLoraNetDevice =
            DynamicCast<SatLorawanNetDevice>(gateway->GetDevice(1));
        Ptr<LorawanMacGateway> gwMac = DynamicCast<LorawanMacGateway>(satLoraNetDevice->GetMac());
        // Create new gatewayStatus
        Ptr<LoraGatewayStatus> gwStatus =
            Create<LoraGatewayStatus>(gatewayAddress, netDevice, gwMac);
        m_status->AddGateway(gateway, gatewayAddress, gwStatus);
        break;
    }
    case SatEnums::REGENERATION_NETWORK: {
        // Create new gatewayStatus
        Ptr<LoraGatewayStatus> gwStatus =
            Create<LoraGatewayStatus>(gatewayAddress, netDevice, nullptr);
        m_status->AddGateway(gateway, gatewayAddress, gwStatus);
        break;
    }
    default:
        NS_FATAL_ERROR("Incorrect regeneration mode for LORA");
    }
}

void
LoraNetworkServer::AddNodes(NodeContainer nodes)
{
    NS_LOG_FUNCTION_NOARGS();

    // For each node in the container, call the function to add that single node
    NodeContainer::Iterator it;
    for (it = nodes.Begin(); it != nodes.End(); it++)
    {
        AddNode(*it);
    }
}

void
LoraNetworkServer::AddNode(Ptr<Node> node)
{
    NS_LOG_FUNCTION(this << node);

    // Get the SatLorawanNetDevice
    Ptr<SatLorawanNetDevice> loraNetDevice;
    for (uint32_t i = 0; i < node->GetNDevices(); i++)
    {
        loraNetDevice = DynamicCast<SatLorawanNetDevice>(node->GetDevice(i));
        if (loraNetDevice != nullptr)
        {
            // We found a SatLorawanNetDevice on the node
            break;
        }
    }

    // Get the MAC
    Ptr<LorawanMacEndDeviceClassA> edLorawanMac =
        DynamicCast<LorawanMacEndDeviceClassA>(loraNetDevice->GetMac());

    // Update the NetworkStatus about the existence of this node
    m_status->AddNode(edLorawanMac);
}

bool
LoraNetworkServer::Receive(Ptr<NetDevice> device,
                           Ptr<const Packet> packet,
                           uint16_t protocol,
                           const Address& address)
{
    NS_LOG_FUNCTION(this << packet << protocol << address);

    // Fire the trace source
    m_receivedPacket(packet);

    // Inform the scheduler of the newly arrived packet
    m_scheduler->OnReceivedPacket(packet);

    // Inform the status of the newly arrived packet
    m_status->OnReceivedPacket(packet, address);

    // Inform the controller of the newly arrived packet
    m_controller->OnNewPacket(packet);

    return true;
}

void
LoraNetworkServer::AddComponent(Ptr<LoraNetworkControllerComponent> component)
{
    NS_LOG_FUNCTION(this << component);

    m_controller->Install(component);
}

Ptr<LoraNetworkStatus>
LoraNetworkServer::GetNetworkStatus(void)
{
    return m_status;
}

} // namespace ns3
